package com.qsign.sfrz_android.activity.home.ViewController;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.qsign.sfrz_android.R;
import com.qsign.sfrz_android.base.NewBaseActivity;
import com.qsign.sfrz_android.net.IServiceImpl;
import com.qsign.sfrz_android.net.RequestServiceImpl;
import com.qsign.sfrz_android.publicview.SimpleDialog;

import butterknife.BindView;
import butterknife.OnClick;

public class ChuKuActivity extends NewBaseActivity {
    @BindView(R.id.tuopanedit)
    EditText tuopanedit;
    @BindView(R.id.yuanliaotext)
    TextView yuanliaotext;
    @BindView(R.id.changditext)
    TextView changditext;
    @BindView(R.id.pihaotext)
    TextView pihaotext;
    @BindView(R.id.tbzltext)
    TextView tbzltext;
    @BindView(R.id.chukuedit)
    EditText chukuedit;
    @BindView(R.id.contentlay)
    LinearLayout contentlay;
    JSONObject result;
    @Override
    protected void initTitle(TextView textView) {
        textView.setText("托盘出库");
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_chuku;
    }
    private final static String SCAN_ACTION = "scan.rcv.message";
    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mScanReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction(SCAN_ACTION);
        registerReceiver(mScanReceiver, filter);
    }
    private BroadcastReceiver mScanReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if(tuopanedit.hasFocus()){
                byte[] barocode = intent.getByteArrayExtra("barocode");
                int barocodelen = intent.getIntExtra("length", 0);
                String barcodeStr = new String(barocode, 0, barocodelen);
                tuopanedit.setText(barcodeStr);
                showWait();
                RequestServiceImpl.chuku_query(mMIPHandler, IServiceImpl.CHUKU_QUERY,tuopanedit.getEditableText().toString(),getIntent().getStringExtra("LX"),getIntent().getStringExtra("bizno"));
            }

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        tuopanedit.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event != null
                        && event.getKeyCode() == KeyEvent.KEYCODE_ENTER
                        && event.getAction() == KeyEvent.ACTION_UP) {
                    String monadNo = tuopanedit.getText().toString().trim();
                    if (tuopanedit.getText().toString().trim().length() >0) {
                        showWait();
                        RequestServiceImpl.chuku_query(mMIPHandler, IServiceImpl.CHUKU_QUERY,tuopanedit.getEditableText().toString(),getIntent().getStringExtra("LX"),getIntent().getStringExtra("bizno"));
                    }
                    return true;
                }
                return false;
            }
        });

        tuopanedit.setFocusableInTouchMode(true);
        tuopanedit.requestFocus();


        chukuedit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s==null){
                    return;
                }
                String temp=s.toString();
                int posDot=temp.indexOf(".");
                int index=chukuedit.getSelectionStart();
                if(posDot>=0&&temp.length()-3>posDot){
                    s.delete(index-1,index);
                }
            }
        });
    }



    @Override
    public void onPostHandle(int requestType, Object objBody, boolean error) throws Exception {
        stopWait();
        String msg = "";
        String opt = "";
        switch (requestType) {
            case IServiceImpl.CHUKU_QUERY://根据条件查询
                try {
                    JSONObject jsonObject = JSONObject.parseObject(objBody.toString());
                    msg = jsonObject.getString("MSG_INFO");
                    opt = jsonObject.getString("OPT_FLAG");
                    if ("SUCCESS".equals(opt)) {
                        result=jsonObject.getJSONObject("DATA");
                        yuanliaotext.setText(result.getString("SZXX"));
                        changditext.setText(result.getString("ORIPLACE"));
                        pihaotext.setText(result.getString("BATCHNO"));
                        tbzltext.setText(result.getString("TPZL"));
                        contentlay.setVisibility(View.VISIBLE);
                    } else {
                        contentlay.setVisibility(View.GONE);
                        if (TextUtils.isEmpty(msg)) {
                            showSimpleDialog("查询出错");
                        } else {
                            showSimpleDialog(msg);
                        }
                        tuopanedit.setText("");

                    }

                } catch (Exception e) {
                }
                break;

            case IServiceImpl.CHUKUIN:
                try {
                    JSONObject jsonObject = JSONObject.parseObject(objBody.toString());
                    msg = jsonObject.getString("MSG_INFO");
                    opt = jsonObject.getString("OPT_FLAG");
                    if ("SUCCESS".equals(opt)) {
                        showSimpleDialog("出库成功");
                        contentlay.setVisibility(View.GONE);
                        tuopanedit.setText("");
                        finish();
                    } else {
                        if (TextUtils.isEmpty(msg)) {
                            showSimpleDialog("出库失败");
                        } else {
                            showSimpleDialog(msg);
                        }
                        tuopanedit.setText("");
                    }

                } catch (Exception e) {
                }
                break;

        }
    }
    private void showSimpleDialog(String message) {
        SimpleDialog.show(this, message);
    }
    @OnClick(R.id.nextbtn)
    public void onViewClicked() {
        if(tuopanedit.getEditableText().toString().length()==0){
            showSimpleDialog("请输入托盘编号");
            return;
        }

        if(chukuedit.getEditableText().toString().length()==0){
            showSimpleDialog("请输入出库重量");
            return;
        }
        if(!TextUtils.isEmpty(result.getString("TPZL"))&&Double.parseDouble(chukuedit.getEditableText().toString())>Double.parseDouble(result.getString("TPZL")))
       {
           showSimpleDialog("出库重量不能大于托盘重量");
           return;
        }

        showWait();
        RequestServiceImpl.chuku(mMIPHandler, IServiceImpl.CHUKUIN,chukuedit.getEditableText().toString(),tuopanedit.getEditableText().toString(),result);
    }
}
